package me.xADudex.RandomLocation;

import org.bukkit.entity.*;
import org.bukkit.*;
import org.bukkit.command.*;
import java.util.*;
import org.bukkit.plugin.*;

class PortalManager
{
    private Main pl;
    
    PortalManager(final Main main) {
        this.pl = main;
    }
    
    void startTask() {
        Bukkit.getScheduler().scheduleSyncRepeatingTask((Plugin)this.pl, (Runnable)new Runnable() {
            @Override
            public void run() {
                for (final Zone z : PortalManager.this.pl.zoneManager.getPortals()) {
                    if (z.getWorld() != null) {
                        for (final Player p : z.getWorld().getPlayers()) {
                            if (!PortalManager.this.pl.timer.portalWalkersContains(p) && z.portalCheck(p)) {
                                if (p.hasPermission("RandomLocation.Use") || p.hasPermission("RandomLocation.Admin") || PortalManager.this.pl.getConfig().getBoolean("Permissions.OpenPortals") || p.hasPermission("RandomLocation.UsePortal")) {
                                    if (z.getPortalDest().equalsIgnoreCase("none")) {
                                        p.sendMessage(ChatColor.GREEN + "No portal destination set!");
                                        PortalManager.this.pl.timer.addPortalWalkers(p);
                                    }
                                    else {
                                        PortalManager.this.pl.zoneManager.teleportPlayer(p, (CommandSender)p, z.getPortalDest(), TeleportSource.PORTAL);
                                        PortalManager.this.pl.timer.addPortalWalkers(p);
                                    }
                                }
                                else {
                                    p.sendMessage(ChatColor.DARK_RED + "You don't got permission to use that portal!");
                                    PortalManager.this.pl.timer.addPortalWalkers(p);
                                }
                            }
                        }
                    }
                }
            }
        }, 0L, 1L);
    }
}
